
//声明函数
//1. 普通函数声明
function add(a: number, b: number): number{
  return a + b;
}

//2. 函数表达式 『匿名函数赋值』
let minus = function(a: number, b: number){
  return a - b;
}

//3. 箭头函数赋值
let times = (x: number, y: number): number => {
  return x * y;
}

//函数类型变量的类型声明
let divide: (a: number, b: number) => number; 
divide = (x: number, y: number) => {
  return x / y;
}

//指数运算 2^3
let pow: (x: number, y: number) => number = (a: number, b: number) => {
  return Math.pow(a, b);
}
// console.log(pow(2, 3));


//函数参数  用来截取字符串
function slice(str: string, start: number, end ?: number){
  return str.slice(start, end);
}

// console.log(slice('iloveyou', 1, 5));
// console.log(slice('iloveyou', 1));

//参数的默认值
function buildPhone(code: string, area: string='+86'){
  return area + code;
}
console.log(buildPhone('15869584723', '+86'));
console.log(buildPhone('18456235874'));
console.log(buildPhone('25874562232', '+260'));

//计算所有参数的和  rest 参数
function jia(...args: number[]){
  return args.reduce((prev, cur) => prev + cur);
}

console.log(jia(1, 2));
console.log(jia(200, 345, 908));

